package vip.xiaonuo.tasks;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import vip.xiaonuo.core.timer.TimerTaskRunner;
import vip.xiaonuo.core.util.DateUtils;
import vip.xiaonuo.facedoor.modular.synchronizelog.entity.SynchronizeLog;
import vip.xiaonuo.facedoor.modular.synchronizelog.service.SynchronizeLogService;

import java.util.Date;

@Component
@Slf4j
public class CleanLogTask implements TimerTaskRunner {

  @Autowired
  private SynchronizeLogService synchronizeLogService;

  @Override
  public void action(String taskId,String taskParam) {
    execute();
  }

  private void execute() {
    log.info("开始删除两天前操作日志！");
    Date date = DateUtils.addDay(new Date(), -2);
    synchronizeLogService.remove(new LambdaQueryWrapper<SynchronizeLog>().le(SynchronizeLog::getCreateDate, date));
  }

  @Override
  @XxlJob("CleanLogTask")
  public void executeXxlJob() {
    execute();
  }
}
